﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;

namespace RFIDStockManagement.Client
{
    public partial class StockPlaceEdit : XtraForm
    {
        private StockPlaceInfo m_StockPlaceInfo = null;
        private long m_Id = 0;
        private long m_StockId = 0;
        public StockPlaceEdit(long stockId, long id)
        {
            InitializeComponent();
            m_StockId = stockId;
            m_Id = id;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length == 0 ||
                    txtNumber.Text.Trim().Length == 0)
                {
                    AlertForm.ShowMessage("仓位名称、编码都不能为空");
                    return;
                }

                if (m_StockPlaceInfo == null)
                {
                    m_StockPlaceInfo = new StockPlaceInfo();
                    m_StockPlaceInfo.StockId = m_StockId;
                }
                m_StockPlaceInfo.Name = txtName.Text.Trim();
                m_StockPlaceInfo.Number = txtNumber.Text.Trim();
                m_StockPlaceInfo.Shelf = txtShelf.Text.Trim();
                m_StockPlaceInfo.Area = txtArea.Text.Trim();

                m_StockPlaceInfo = BasicInfoManager.Save(m_StockPlaceInfo, EnumBasicInfo.StockPlace) as StockPlaceInfo;

                AlertForm.ShowMessage("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void StockPlaceEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                BindData(m_Id);
            }
        }
        private void BindData(long id)
        {
            try
            {
                m_StockPlaceInfo = BasicInfoManager.GetOne(id, EnumBasicInfo.StockPlace) as StockPlaceInfo;
                if (m_StockPlaceInfo == null)
                {
                    AlertForm.ShowMessage("读取不出数据，可能被删除");
                    this.Close();
                    return;
                }
                txtName.Text = m_StockPlaceInfo.Name;
                txtNumber.Text = m_StockPlaceInfo.Number;
                txtShelf.Text = m_StockPlaceInfo.Shelf;
                txtArea.Text = m_StockPlaceInfo.Area;
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
                this.Close();
            }
        }
    }
}